package com.atguigu.gulimall.order.service.impl;

import com.atguigu.gulimall.order.dao.OrderItemDao;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderItemEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.order.service.OrderItemService;


@Service("orderItemService")
@RabbitListener(queues = {"hello.java.queue"})
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * queues：声明监听到的所有队列
     * <p>
     * 参数可写以下类型
     * 1、Message message：原生消息信息，消息头+消息体
     * 2、T<发送的消息的类型> OrderReturnReasonEntity entity
     * 3、Channel channel，当前传输数据的通道
     * <p>
     * Queue:可以很多人都来监听，只要收到消息，队列删除消息【只能有一个人接收到】
     * 场景：
     * 1）、订单服务启动多个，同一个消息只能有一个收到
     * 2）、业务处理很耗时，一个消息处理完才能接收下一个消息
     */
    @RabbitHandler
    public void receiveMessage(OrderReturnReasonEntity entity, Message message, Channel channel) throws IOException, InterruptedException {
        System.out.println(entity);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //签收消息，非批量模式【channel]
        channel.basicAck(deliveryTag, false);
        //拒绝消息，非批量模式【channel]，不重新入队
//        channel.basicNack(deliveryTag,false,false);
    }

    @RabbitHandler
    public void receiveMessage(OrderEntity entity) throws InterruptedException {
        System.out.println(entity);
    }

}